package com.woniuxy.woniuticket.usermodule.controller;


import com.woniuxy.woniuticket.movieinfomodule.service.impl.UserInfoServiceImp;
import com.woniuxy.woniuticket.pojo.UsersInfo;
import com.woniuxy.woniuticket.pojo.UsersLogin;
import com.woniuxy.woniuticket.usermodule.constant.Constant;
import com.woniuxy.woniuticket.usermodule.service.Imp.UserLoginServiceImp;
import com.woniuxy.woniuticket.usermodule.utils.GetMd5;
import com.woniuxy.woniuticket.usermodule.utils.RegexUtil;
import com.woniuxy.woniuticket.usermodule.utils.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.security.MessageDigest;
import java.util.Random;

@Controller
@RequestMapping("/login")
public class UserLoginController {
    @Autowired
    private UserLoginServiceImp userLoginServiceImp;
    @Resource
    private UserInfoServiceImp userInfoServiceImp;
    //用于登录信息反馈
    private String message;


    //登录
    @RequestMapping("/login")
    @ResponseBody
    public String login(UsersLogin user,String verificationCode,HttpSession session){
        System.out.println(user);
        if (!RegexUtil.isMobile(user.getAccount())) {//验证用户名是否合法
            message = "用户名不合法";
        }else{
            //从session中获取账户对应的验证码
            Integer code = (Integer) session.getAttribute(user.getAccount());
            if(code != null){//验证码不为空，说明已经点击了获取验证码
                if (code.toString().equals(verificationCode)){//验证码正确再验证用户身份信息
                    if(user.getPassword() != null){
                        user.setPassword(GetMd5.makeMd5(user.getPassword()));
                    }

                    message = userLoginServiceImp.login(user);

                    if("登录成功".equals(message)){
                        UsersInfo usersInfo = userInfoServiceImp.getUserInfoId(user.getAccount());
                        //存入用户信息
                        session.setAttribute("user",usersInfo);
                    }

                }else{
                    message = "验证码错误";
                }
            }else{
                message = "请点击手机验证";
            }

        }
        return message;
    }

    //注册
    @RequestMapping("/regist")
    @ResponseBody
    public String regist(UsersLogin user,String verificationCode,HttpSession session){
        if (!RegexUtil.isMobile(user.getAccount())) {//验证用户名是否合法
            message = "用户名不合法";
        }else{
            //从session中获取账户对应的验证码
            Integer code = (Integer) session.getAttribute(user.getAccount());
            if(code != null){//验证码不为空，说明已经点击了获取验证码
                if (code.toString().equals(verificationCode)){//验证码正确再验证用户身份信息
                    if(user.getPassword() != null){
                        user.setPassword(GetMd5.makeMd5(user.getPassword()));
                    }
                    message = userLoginServiceImp.regist(user);
                }else{
                    message = "验证码错误";
                }
            }else{
                message = "请点击手机验证";
            }
        }
        return message;
    }

    //通过账户填写账户（account）获取验证码
    @RequestMapping("/getCode")
    @ResponseBody
    public String getCode(String account, HttpSession session){
        System.out.println("获取code");
        System.out.println(account);
        if(account != null && RegexUtil.isMobile(account)){//判断账号是否为电话号码
            Integer code = new Random().nextInt(1000000);
            System.out.println(code);
            //将验证码存入session中，key为用户名（电话号码），value为验证码code
            session.setAttribute(account,code);
            try {
                //发送短信
                SMSUtil.sendSms(account,code.toString(), Constant.SMSTemplate1);
                message = "验证中";
            } catch (Exception e) {
                message = "验证失败，请重新点击";
                e.printStackTrace();
            }
        }else{
            message = "用户名不合法";
        }
        return message;
        //return "";
    }

    //修改密码
    @RequestMapping("/changePas")
    @ResponseBody
    public String changePas(UsersLogin user,String verificationCode,HttpSession session){
        if (!RegexUtil.isMobile(user.getAccount())) {//验证用户名是否合法
            message = "用户名不合法";
        }else{
            //从session中获取账户对应的验证码
            Integer code = (Integer) session.getAttribute(user.getAccount());
            if(code != null){//验证码不为空，说明已经点击了获取验证码
                if (code.toString().equals(verificationCode)){//验证码正确再验证用户身份信息
                   if(user.getPassword() != null){
                       user.setPassword(GetMd5.makeMd5(user.getPassword()));
                   }
                    message = userLoginServiceImp.updatePas(user);
                    if("修改成功".equals(message)){
                        UsersInfo usersInfo = userInfoServiceImp.getUserInfoId(user.getAccount());
                        //存入用户信息
                        session.setAttribute("user",usersInfo);
                    }
                }else{
                    message = "验证码错误";
                }
            }else{
                message = "请点击手机验证";
            }
        }
        return message;
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        UsersInfo user = (UsersInfo) session.getAttribute("user");
        if(user != null){
            session.removeAttribute("user");
        }
        return "redirect:/movieshow/index.html";
    }
}
